package cn.edu.cug.cs.gtl.docsrv.auth.dao.imp;

import cn.edu.cug.cs.gtl.docsrv.auth.dao.UserDao;
import cn.edu.cug.cs.gtl.docsrv.config.Configuration;
import cn.edu.cug.cs.gtl.mybatis.Session;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthUser;
import cn.edu.cug.cs.gtl.mybatis.mapper.auth.AuthUserMapper;
import org.springframework.stereotype.Component;

@Component
public class UserDaoImpl implements UserDao {
    @Override
    public boolean insertUser(AuthUser authUser) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthUserMapper authUserMapper = s.getMapper(AuthUserMapper.class);
            authUserMapper.insert(authUser);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean updateUser(AuthUser authUser) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthUserMapper authUserMapper = s.getMapper(AuthUserMapper.class);
            authUserMapper.updateByUserName(authUser);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUser(String authUserName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthUserMapper authUserMapper = s.getMapper(AuthUserMapper.class);
            authUserMapper.deleteByUserName(authUserName);
            s.close();
            return true;
        }
        return false;
    }

    @Override
    public AuthUser getUser(String authUserName) {
        Session s = Configuration.getSession();
        if(s!=null && !s.isClosed()){
            AuthUserMapper authUserMapper = s.getMapper(AuthUserMapper.class);
            AuthUser authUser = authUserMapper.selectByUserName(authUserName);
            s.close();
            return authUser;
        }
        return null;
    }
}
